package com.ldr.danmuku

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.children
import com.ldr.danmuku.item.DanMuItemView
import com.ldr.danmuku.item.DanMuTextBean
import com.ldr.danmuku.item.DanMuTextItemView

/**
 * @author :create by ldr
 * time:2022/12/28 16:12
 * @descript:弹幕泳道的View
 */
class DanMuLaneView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    :FrameLayout(context, attrs, defStyleAttr) {

    companion object {
        private const val TAG = "DanMu"
    }

    private val mDanMuConfig = DanMuManager.singleton.mDanMuConfig
    private var mCurrentHeight = 0
    private var mCurrentWith = 0
    private val mArrayList:ArrayList<DanMuItemView> = ArrayList<DanMuItemView>()

    var hasInitCanAddView = false

    init {
        setPadding(
            0,
            mDanMuConfig.mLanViewPaddingTop.toInt(),
            0,
            mDanMuConfig.mLanViewPaddingBottom.toInt()
        )
        judgeDanMuItemViewHeight()
    }

    //计算一下本View的宽高
    private val mRunnable = Runnable {
        mCurrentWith = this@DanMuLaneView.width
        mCurrentHeight = this@DanMuLaneView.height
        hasInitCanAddView = true
        startAddView()
    }

    /**
     * 可以开始增加View了
     */
    private fun startAddView() {
        LanViewManager.singleton.startAnimal()
        addItemView(DanMuTextItemView(DanMuTextBean("测试数据11111",null,null),context))
    }


    /**
     * 计算一下弹幕的itemView的高度
     */
    fun judgeDanMuItemViewHeight() {
        val danMuTextItemView = DanMuTextItemView(DanMuTextBean(" ", null, null), context)
        mCurrentHeight = danMuTextItemView.mCurrentWidthAndHeightPair.second.toInt()
        setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent))
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val finalHeight =
            mCurrentHeight + mDanMuConfig.mLanViewPaddingTop + mDanMuConfig.mLanViewPaddingBottom
        this.setMeasuredDimension(ViewGroup.LayoutParams.MATCH_PARENT, finalHeight.toInt())
        Log.d(TAG, "onMeasure:------------------ ")
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        val layoutParams = (layoutParams as LinearLayout.LayoutParams)
        val marTop = layoutParams.topMargin
        if (marTop <= 0) {
            layoutParams.topMargin = mDanMuConfig.mLanViewMarginTop.toInt()
            layoutParams.gravity = Gravity.CENTER_VERTICAL
        }
        post(mRunnable)
    }

    fun addItemView(danMuItemView:DanMuItemView){
        this.addView(danMuItemView.getView())
        mArrayList.add(danMuItemView)
    }

    fun removeItemView(danMuItemView:DanMuItemView){
        removeView(danMuItemView.getView())
        mArrayList.remove(danMuItemView)
    }

    /**
     * 更新View的当前位置
     * 如果已經走到尽头则清除掉该数据
     * 否则更新该View
     */
    fun updateViewLayout() {
        val iterator = children.iterator()
        while (iterator.hasNext()){
           val item = iterator.next()
            val danMuItemView = item as DanMuItemView
            if (danMuItemView.getLocalEndPosition() <= 0){
                removeItemView(danMuItemView)
            }else{
                danMuItemView.updateCurrentLocalLayout()
            }
        }
    }
}

